package com.admin.action.bookaction;

import com.admin.Action;
import com.admin.dto.BookLossBean;
import com.admin.service.bookservice.BookServiceImpl;
import com.admin.service.bookservice.BookServiceInF;
import com.admin.service.borservice.BorServiceImpl;
import com.pojo.BorrowerBean;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

public class ChuLiBookAction extends Action {
    @Override
    public void execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //borname,degree

        String id=request.getParameter("id");
        String degree=request.getParameter("degree");

        BookServiceImpl bookService = new BookServiceImpl();
// <option>遗失</option>
//                                            <option>轻微破损</option>
//                                            <option>明显破损</option>
//                                            <option>完全破损</option>

        PrintWriter out=response.getWriter();


        HttpSession session = request.getSession();
//        System.out.println(session.getAttribute("adminInfo"));
        if(session.getAttribute("adminInfo")==null){
            out.println("<script>parent.window.location.href='error.html';</script>");
            return;
        }



        String zhuangtai =bookService.demageDegree(Integer.valueOf(id));
        System.out.println(degree.equals("遗失"));
        //得到借阅者idCard
        List<BookLossBean> beanList= bookService.queryBookDemage();

        BorrowerBean bor = new BorrowerBean();
        String idCard=null;
        Double price=null;
        for(BookLossBean bl:beanList){
            idCard=bl.getIdcard();
            price=bl.getPrice();
        }
        bor.setIdCard(idCard);


        BorServiceImpl service = new BorServiceImpl();
        System.out.println("900000"+degree.equals("遗失"));

        double kouchu=0-price;

        if(degree.equals("遗失")  ||  degree.equals("完全破损")){
            //扣除书的价值
            bor.setBalance(kouchu);
            boolean bool = service.updateBorBalance(bor);
            if(bool){
                out.print("扣除"+price+"元");
            }else {
                out.print(false);
            }
        }else if(degree.equals("轻微破损")){
            bor.setBalance(kouchu/5);
            boolean bool = service.updateBorBalance(bor);
            if(bool){
                out.print("扣除"+price/5+"元");
            }else {
                out.print(false);
            }
        }else {
            out.print(false);
        }





    }
}
